package com.maridress.maridress.model.adapter;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.maridress.maridress.R;
import com.maridress.maridress.constant.Constant;
import com.maridress.maridress.model.HttpResult;
import com.maridress.maridress.model.UserResponse;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tenmei on 16/9/7.
 */
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.Holder> {

    private final LayoutInflater mInflater;
    private List<UserResponse> mUserList;
    private UserClickListener mListener;

    public UserAdapter(UserClickListener listener, LayoutInflater inflater) {
        mListener = listener;
        mInflater = inflater;
        mUserList = new ArrayList<>();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(mInflater.inflate(R.layout.item_user, parent, false));
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {

        UserResponse currUser = mUserList.get(position);

        holder.mName.setText(currUser.getUsername());
        holder.mHeadline.setText(currUser.getHeadline());
//        System.out.printf(Constant.BASE_IMAGE_URL + currUser.getAvatar());
        Glide.with(holder.itemView.getContext()).load(Constant.BASE_IMAGE_URL + currUser.getAvatar()).into(holder.mPhoto);
    }

    @Override
    public int getItemCount() {
        return mUserList.size();
    }

    public void addUsers(HttpResult<List<UserResponse>> flowerResponses) {
        System.out.printf(flowerResponses.getData().get(1).getUsername());
        mUserList.addAll(flowerResponses.getData());
        notifyDataSetChanged();
    }

    public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView mPhoto;
        private TextView mName, mHeadline;

        public Holder(View itemView) {
            super(itemView);

            mPhoto = (ImageView) itemView.findViewById(R.id.userAvatar);
            mName = (TextView) itemView.findViewById(R.id.userName);
            mHeadline = (TextView) itemView.findViewById(R.id.userHeadline);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(getLayoutPosition(), mUserList.get(getAdapterPosition()).getUsername());
        }
    }

    public interface UserClickListener {
        void onClick(int position, String name);
    }

}
